package com.em.frame.utils;

import java.util.*;

/**
 * Created by zlx on 2017/11/23 0023.
 */
public class StringUtil {

    /**
     * 是否为空
     * @param value
     * @return
     */
    public static boolean isEmpty(CharSequence value) {
        return value == null || value.length() == 0;
    }

    /**
     * 获取唯一序列号
     * @return
     */
    public static String getUUID() {
        UUID uid = UUID.randomUUID();
        String str = uid.toString();
        return str.substring(0, 8) + str.substring(9, 13) + str.substring(14, 18) + str.substring(19, 23) + str.substring(24);
    }


    /**
     * 把数组所有元素按照ASCII码从小到大排序，并按照“参数=参数值”的模式用“&”字符拼接成字符串
     * @param params 需要排序并参与字符拼接的参数组
     * @return 拼接后字符串
     */
    public static String createLinkString(Map<String, String> params) {
        List<String> keys = new ArrayList<String>(params.keySet());
        Collections.sort(keys);
        String prestr = "";
        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            String value = params.get(key);
            if (i == keys.size() - 1) {//拼接时，不包括最后一个&字符
                prestr = prestr + key + "=" + value;
            } else {
                prestr = prestr + key + "=" + value + "&";
            }
        }
        return prestr;
    }

    public static void main(String[] args) {
        System.out.printf(getUUID());
    }
}
